package com.mall.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.mall.auth.entity.Oauth2TokenDto;
import com.mall.common.constant.AuthConstant;
import com.mall.common.entity.ResponseData;
import com.mall.user.api.UserApi;
import com.mall.user.entity.param.UserAddParam;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.security.KeyPair;
import java.security.Principal;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

/**
 * @className: com.mall.com.mall.auth.controller-> AuthController
 * @description:
 * @author: wuyijie
 * @createDate: 2021/6/22 22:12
 */
@RestController
@Api(value = "AuthController",  tags= "认证中心登录认证")
@RequestMapping("/auth")
@Slf4j
public class AuthController {
    @DubboReference
    private UserApi userApi;

    @Autowired
    private KeyPair keyPair;

    @GetMapping("/rsa/publicKey")
    @ApiOperation(value = "getPublicKey",notes = "暴露公钥接口")
    public Map<String, Object> getKey() {
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAKey key = new RSAKey.Builder(publicKey).build();
        return new JWKSet(key).toJSONObject();
    }

    @PostMapping("/register")
    public ResponseData register(@RequestBody UserAddParam userAddParam){
        return ResponseData.success(userApi.add(userAddParam));
    }

//    @Autowired
//    private TokenEndpoint tokenEndpoint;

//    @ApiOperation("Oauth2获取token")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "grant_type", value = "授权模式", required = true),
//            @ApiImplicitParam(name = "client_id", value = "Oauth2客户端ID", required = true),
//            @ApiImplicitParam(name = "client_secret", value = "Oauth2客户端秘钥", required = true),
//            @ApiImplicitParam(name = "refresh_token", value = "刷新token"),
//            @ApiImplicitParam(name = "username", value = "登录用户名"),
//            @ApiImplicitParam(name = "password", value = "登录密码")
//    })
//    @RequestMapping(value = "/token", method = RequestMethod.POST)
//    public ResponseData token(@ApiIgnore Principal principal, @ApiIgnore @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
//        log.info(JSONObject.toJSONString(principal));
//        OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
//        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.builder()
//                .token(oAuth2AccessToken.getValue())
//                .refreshToken(oAuth2AccessToken.getRefreshToken().getValue())
//                .expiresIn(oAuth2AccessToken.getExpiresIn())
//                .tokenHead(AuthConstant.JWT_TOKEN_PREFIX).build();
//        return ResponseData.success(oauth2TokenDto);
//    }

    @ApiOperation("test1")
    @RequestMapping(value = "/test1",method = RequestMethod.GET)
    public String test1(){
        return "test1";
    }

    @ApiOperation("test2")
    @RequestMapping(value = "/test2",method = RequestMethod.GET)
    public String test2(){
        return "test2";
    }
}